package com.rdc.example.service.impl;

import com.rdc.example.bean.Result;
import com.rdc.example.dao.FollowDao;
import com.rdc.example.dao.impl.FollowDaoImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FollowServiceImpl implements com.rdc.example.service.FollowService {
    FollowDao followDao = new FollowDaoImpl();

    @Override
    public Result turnFollow(Integer followerId, Integer followedId) throws Exception {
        if(followerId == null || followedId == null) {
            return Result.fail("followerId或followedId不为空", null);
        }
        if(followerId.equals(followedId)) {
            return Result.fail("不能关注自己", null);
        }
        if(followDao.check(followerId, followedId)) {
            //关注了，取关
            boolean delete= followDao.deleteFollow(followerId, followedId);
            return delete ? Result.success("取关成功", null)
                    :Result.fail("取关失败",null);
        }else{
            //未关注 ，关注
            boolean insert= followDao.insertFollow(followerId, followedId);
            return insert ? Result.success("关注成功", null)
                    :Result.fail("关注失败",null);
        }
    }

    @Override
    public Boolean check(Integer followerId, Integer followedId) throws Exception {
        if(followerId == null || followedId == null) {
            return null;
        }
        return followDao.check(followerId, followedId);
    }

    @Override
    public Map<Integer, Boolean> batchCheck(Integer followerId, List<Integer> followedIds) throws Exception {
        if (followerId == null || followedIds == null) {
            return null;
        }
        Map<Integer, Boolean> result = new HashMap<>();
        for (Integer followedId : followedIds) {
            if (followedId != null) {
                Boolean status = check(followerId, followedId);
                //强转布尔值
                result.put(followedId, Boolean.TRUE.equals(status));
            }
        }
        return result;
    }
}
